<?php
namespace app\admin\controller;
use think\Db;
class Adminrole extends Base
{   
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    admin_role
                where 
                    1 = ?
                ";
            $param = [1];
            if ( !empty($keyword) ) {
                $sql .= " and title like ?";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
			$page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by id desc limit $start, $page_size";
			$field       = "*";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['action']         =  com_action_edit( url("adminrole/edit","id={$v['id']}"), $v['id'], '编辑部门' );
                $list[$k]['action']        .=  com_action_del( url("adminrole/delete"), $v['id'], "你确定要删除{$v['title']}吗？" );
            }
			$com_body    = com_build_table($list, array("checkbox","id","title", "create_time", "action"));
			$com_page    = com_build_page( $page_data );
			$data = array(
				"com_body"  => $com_body,
				"com_page"  => $com_page,
			);
			com_check(['data'=>$data]);
        }       
        $data = [
            'nav'       => ['部门管理', '列表'],
        ];
        return view('', $data);
    }
    
    //添加
    public function add(){
        $a  = new \app\admin\model\AdminPower();
        if( is_post() ){
            $data = input();  #post数据
            $old  = Db::name('admin_power')->column('id');
            if( isset($data['ids']) ){
                $new = array_filter($data['ids']);
                unset($data['ids']);
            }else{
                $new = [];
            }
            $arr  = array_diff($old, $new);
            $data['content']     = ",".implode(",", $arr).",";
            $data['create_time'] = time();
            
            //验证
            $rule = [
                'title'      =>  'require',
            ];
            $msg = [
                'title.require'      =>  '标题不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            Db::name('admin_role')->insertGetId($data);
            $config = [
                'msg' => '添加部门成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        } 
        $data = [
            'ids' => Db::name('admin_power')->column('id'),
        ];
        return view('', $data);
    }
    
    
    //编辑
    public function edit( $id ){
        $a  = new \app\admin\model\AdminPower();
        if( is_post() ){
            $data = input();  #post数据
            $old  = Db::name('admin_power')->column('id');
            if( isset($data['ids']) ){
                $new = array_filter($data['ids']);
                unset($data['ids']);
            }else{
                $new = [];
            }
            $arr  = array_diff($old, $new);
            $data['content']     = ",".implode(",", $arr).",";
            $data['update_time'] = time();
            //验证
            $rule = [
                'title'      =>  'require',
            ];
            $msg = [
                'title.require'      =>  '标题不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            Db::name('admin_role')->update($data);
            $config = [
                'msg' => '修改部门成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        } 
        $rs = Db::name('admin_role')->find($id);
        $data = [
            'ids' => explode(",", $rs['content']),
            'id'  => $id,
            'title' => $rs['title'],
        ];
        return view('', $data);
    }
    


    //删除
    public function delete(){
        if( is_post() ){
            $id  =  input('id');
            $a   = new \app\admin\model\AdminRole();
    
            if( $a->checkDel($id) )
                Db::name('admin_role')->delete($id);
    
            $config = [
                'suc' => [
                    'msg'       => '删除成功!',
                    'js_action' => 'alert|reload',
                ],
                'err' => [
                    'js_action' => 'alert',
                ]
            ];
            com_check($config);
        }
    }
    
    //批量删除
    public function delall(){
        if( is_post() ){
    
            $ids = input('ids/a');
            $a   = new \app\admin\model\AdminRole();
    
            foreach($ids as $id){
                if( $a->checkDel($id) ){
                    Db::name('admin_role')->delete($id);
                }
            }
            $config = [
                'err' => [
                    'js_action' => "alert|reload",
                ],
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check($config);
        }
    }
}